package cn.xjt.TcpFileLoad;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author xujiangtao
 * @create 2020-08-04-15:10
 */
public class ServerFileUpload {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(new File("E:\\AE Folder\\苹果.ai"));
        ServerSocket server = new ServerSocket(9999);
        System.out.println("等待客户端连接");
        Socket client = server.accept();
        System.out.println("连接成功");
        InputStream serIS = client.getInputStream();
        byte[] data = new byte[5000];
        int lenght = -1;
        System.out.println("数据正在上传...");
//        lenght=serIS.read(data);
//        System.out.println(lenght);
//        fileOutputStream.write(data,0,lenght);
        while ((lenght=serIS.read(data))!=-1)
        {
            if(lenght<data.length)
            System.out.println(lenght+"#######################"+lenght);
            System.out.println("再循环上传");
            fileOutputStream.write(data,0,lenght);
//            if(lenght<5000)       //判断如果读取的长度小于字节长度，则跳出循环
//            {
//                break;            //因客户端使用结束次套接次的输出流，所以不用加次判断跳出
//            }
        }
        System.out.println("数据已经上传成功");
//        serIS.read(data);

//        fileOutputStream.write(data);
        OutputStream os = client.getOutputStream();
        os.write("上传成功".getBytes());
    }
}